package com.hitgo.soccer.config;

import java.util.HashMap;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class PositionConfig
{
	public static final int FORWARD	= 30;			//	forward / striker 前锋 
	public static final int	MIDFIELDER	=20;			//	midfielder 前卫 
	public static final int LEFT_MIDFIELDER 	= 21;	//	left midfielder 左前卫 
	public static final int RIGHT_MIDFIELDER	= 25;	//	right midfielder 右前卫 
	//	attacking midfielder 攻击型前卫（前腰） 
	//	defending midfielder 防守型前卫（后腰） 
	public static final int CENTER_FORWARD	= 23;		//	center forward 中锋 
	public static final int FULL_BACK	= 10;			//	full back 后卫 
	public static final int LEFT_BACK 	= 11;			//	left back 左后卫 
	public static final int CENTER_BACK = 13;		//	center back 中后卫 
	public static final int RIGHT_BACK 	= 15;			//	right back 右后卫 
	//	sweeper 清道夫，拖后中卫 
	public static final int GOALKEEPER = 0;//	goalkeeper / goalie 守门员 

	public short positionType;
	public short initX;
	public short initY;

	public short rangTopX;
	public short rangTopY;
	public short rangBottomX;
	public short rangBottomY;
	
	public static HashMap<Short, PositionConfig> positionMap = new HashMap<Short, PositionConfig>();
	
	public static HashMap<String, Short[]> formationMap = new HashMap<String, Short[]>();
	
	public static void loadConfig(String configFile)
	{
		if (configFile == null || configFile.isEmpty())
			return;
			
		// 获取文档的根元素，赋值给rootElement变量
		Element rootElement = ConfigUtil.GetRootElement(configFile);
		if(rootElement == null)
			return ;
		
		NodeList nodePostions = rootElement.getElementsByTagName("position");
		int nSize = nodePostions.getLength();
		
		for(int nIndex = 0; nIndex < nSize; nIndex++)
		{
			Node nodePosition = nodePostions.item(nIndex);
			if(nodePosition == null)
				continue ;
			
			short positionType = (short) ConfigUtil.GetAttributeIntValue(nodePosition, "type");
			short initX = (short) ConfigUtil.GetAttributeIntValue(nodePosition, "init_x");
			short initY = (short) ConfigUtil.GetAttributeIntValue(nodePosition, "init_y");
			String strRange = ConfigUtil.GetAttributeStrValue(nodePosition, "range");
			String[] aRange =  strRange.split(",");
			
			PositionConfig pType = new PositionConfig();
			pType.positionType = positionType;
			pType.initX = initX;
			pType.initY = initY;
			
			pType.rangTopX = Short.parseShort(aRange[0]);
			pType.rangTopY = Short.parseShort(aRange[1]);
			pType.rangBottomX = Short.parseShort(aRange[2]);
			pType.rangBottomY = Short.parseShort(aRange[3]);
			
			positionMap.put(positionType, pType);
		}
		
		NodeList nodeFormations = rootElement.getElementsByTagName("formation");
		
		for(int nIndex = 0; nIndex < nodeFormations.getLength(); nIndex++)
		{
			Node nodeFormation = nodeFormations.item(nIndex);
			if(nodeFormation == null)
				continue ;
			String formationID = ConfigUtil.GetAttributeStrValue(nodeFormation, "type");
			String players = ConfigUtil.GetAttributeStrValue(nodeFormation, "playes");
			String[] pa = players.split(",");
			Short[] spa = new Short[pa.length];
			for(int nPA = 0; nPA < pa.length; nPA++)
			{
				spa[nPA] = Short.parseShort(pa[nPA].trim());
			}
			
			formationMap.put(formationID, spa);
		}
	}
	
	public static PositionConfig getPosition(Short position)
	{
		return positionMap.get(position);
	}
}
